<?php

namespace addons\workteam\library\notice;

use think\Exception;

class Notice
{

    /**
     * 配置信息
     * @var array
     */
    private $config = [];

    /**
     * 服务提供者
     * @var array
     */
    private $providers = [
        'Email' => 'Email',
        'WXWork' => 'WXWork',
        'Dingding' => 'Dingding',
    ];
    protected static $instance = null;

    private $engine;    // 当前引擎类

    public function __construct($options = [])
    {
        $this->config = array_merge($this->config, is_array($options) ? $options : []);

        //注册服务提供者
        if (isset($this->config['key'])){
            $this->engine = $this->registerProviders();
        }

    }
    /**
     *
     * @param array $options 参数
     * @return $this
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }else{
            if (isset($options['key'])){
                self::$instance->engine = self::$instance->registerProviders();
            }
        }

        return self::$instance;
    }

    public function send($config,$data,$replace_data=array())
    {
        try {
            return $this->engine->send($config,$data,$replace_data);
        }catch (Exception $e){
            $this->getError();
            return  false;
        }

    }


    /**
     * 获取通知类型
     * @return array|string[]
     */
    public static function getProviders()
    {
        return (new  self())->providers;
    }

    /**
     * 注册服务提供者
     */
    private function registerProviders()
    {
        $objname = __NAMESPACE__ . "\\engine\\" . $this->providers[$this->config['key']];
        return new $objname($this->config);
    }

    /**
     * 获取默认通知模板数据
     */
    public static function getTempLplData()
    {
        return array(
            [
                'id' => 1,
                'key' => "notice_new_task",
                'describe' => __("新任务通知"),
                'status' => 0,
                'values' => '',
            ],
            [
                'id' => 2,
                'key' => "notice_task_start",
                'describe' => __("任务即将开始"),
                'status' => 0,
                'values' => [
                    'content'=>'',
                    'engine'=>'',
                ],

            ],
            [
                'id' => 2,
                'key' => "notice_expire_task",
                'describe' => __("逾期任务通知"),
                'status' => 0,
                'values' => [
                    'content'=>'',
                    'engine'=>'',
                ],

            ],
            [
                'id' => 3,
                'key' => "notice_expire_project",
                'describe' => __("项目逾期提醒"),
                'status' => 0,
                'values' => [],
            ],
            [
                'id' => 4,
                'key' => "notice_project_log",
                'describe' => __("项目日志提醒"),
                'status' => 0,
                'values' => [
                    'content'=>'项目通知：{:content}',
                ],
            ],
            [
                'id' => 5,
                'key' => "notice_task_log",
                'describe' => __("任务日志提醒"),
                'status' => 0,
                'values' => [
                    'content'=>'任务通知：{:content}',
                ],
            ],
        );
    }

    /**
     * 获取错误信息
     * @return mixed
     */
    public function getError()
    {
        return $this->engine->getError();
    }


}
